﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gasco.DataAccess;
using System.Data.Linq;

namespace Gasco.Business.Services
{
    public class LookupService<T> : ILookupService<T> where T : LINQLookupEntity
    {
        public int CountGetAll()
        {
            using (GascoDBDataContext db = DatabaseHelper.GetData())
            {
                Table<T> table = db.GetTable<T>();
                return table.Count();
            }
        }

        public IList<T> GetAll()
        {
            using (GascoDBDataContext db = DatabaseHelper.GetData())
            {
                Table<T> table = db.GetTable<T>();
                return table.ToList<T>();
            }
        }


        public IList<T> FindByName(string name)
        {
            using (GascoDBDataContext db = DatabaseHelper.GetData())
            {
                Table<T> table = db.GetTable<T>();
                var result = table.Where<T>(c => c.name.Contains(name));
                return result.ToList<T>();
            }
        }

        public T FindById(int id)
        {
            using (GascoDBDataContext db = DatabaseHelper.GetData())
            {
                db.DeferredLoadingEnabled = false;
                Table<T> table = db.GetTable<T>();
                var result = table.Where<T>(c => c.id == id);
                T entity = result.Single<T>();
                entity.SetAsChangeTrackingRoot(true);
                return entity;
            }
        }


        public T Create(T entity)
        {
            return DatabaseHelper.Insert<T>(entity);
        }

        public void Update(T entity)
        {
            DatabaseHelper.Update<T>(entity);
        }

        public void Delete(T entity)
        {
            DatabaseHelper.Delete<T>(entity);
        }

        public void Delete(int id)
        {
            this.Delete(this.FindById(id));
        }

        public IList<T> SearchAll(string keyword, int pageSize, int pageNumber, string sortBy, string sortDirection)
        {
            var db = DatabaseHelper.GetData();
            IQueryable<T> result = null;

            if (String.IsNullOrEmpty(keyword))
            {
                result = from all in db.GetTable<T>()
                         select all;
            }
            else
            {
                result = from all in db.GetTable<T>()
                         where (all.name.Contains(keyword))
                         select all;
            }

            if (sortDirection.ToLower() == "descending")
                sortBy = String.Format("{0} DESC", sortBy);
            return result.Skip((pageNumber - 1) * pageSize).Take(pageSize).SortBy<T>(sortBy).ToList<T>();
        
        }

        public int CountSearchAll(string keyword)
        {
            var db = DatabaseHelper.GetData();
            IQueryable<T> result = null;

            if (String.IsNullOrEmpty(keyword))
            {
                result = from all in db.GetTable<T>()
                         select all;
            }
            else
            {
                result = from all in db.GetTable<T>()
                         where (all.name.Contains(keyword))
                         select all;
            }
            return result.Count();
        }
    }
}
